package cn.ljy.mes.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyServiceException;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.mes.dao.mapper.WmsMaterialBatchMapper;
import cn.ljy.mes.model.WmsMaterialBatch;
import cn.ljy.mes.model.query.WmsMaterialBatchQuery;
import cn.ljy.mes.service.WmsMaterialBatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import java.io.Serializable;
import java.util.List;

@Service
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Validated//表示开启sprint的校检框架，会自动扫描方法里的@Valid（@Valid注解一般写在接口即可）
@Slf4j
public class IWmsMaterialBatchService extends IBaseService<WmsMaterialBatch, WmsMaterialBatchQuery> implements WmsMaterialBatchService {
    @Autowired
    private WmsMaterialBatchMapper mapper;


    @Override
    protected void beforeSave(WmsMaterialBatch wmsMaterialBatch) {
        if(wmsMaterialBatch.getMaterial()==null||wmsMaterialBatch.getMaterial().getId()==null){
            throw new MyWebException("验证失败：请先选择物料");
        }
        if(!StringUtils.hasLength(wmsMaterialBatch.getBatchNumber())){//如果没有输入批次号
            //生成批次号
            wmsMaterialBatch.setBatchNumber(generateBasicBatchNo());
        }
        //库存数量默认等于0
        wmsMaterialBatch.setTotalInventory(0l);
    }

    @Override
    protected void beforeUpdate(WmsMaterialBatch wmsMaterialBatch) {
        WmsMaterialBatch materialBatchOfOrignl=mapper.findByIdNoRelation(wmsMaterialBatch.getId());
        if(wmsMaterialBatch.getPurchaseQuantity()!=null){
            if(wmsMaterialBatch.getPurchaseQuantity()<materialBatchOfOrignl.getStoredQuantity()){
                throw new MyWebException("验证失败：进货数量不能小于已入库数量");
            }
        }
        //批次号不允许修改
        wmsMaterialBatch.setBatchNumber(null);
        //进货数量不允许修改
        wmsMaterialBatch.setPurchaseQuantity(null);
    }

    @Override
    public void afterFindAll(List<WmsMaterialBatch> list) {
        for(WmsMaterialBatch materialBatch:list){
            //添加相关关联属性
            //计算未入库物料
            materialBatch.setPendingQuantity(materialBatch.getPurchaseQuantity()- materialBatch.getStoredQuantity());
            //获取库位数量
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        for(Serializable id:ids){
            WmsMaterialBatch materialBatch=mapper.findById(id);
            if(materialBatch!=null){
                //验证是否已经入库，如果没有入库可以删除
                if(materialBatch.getStoredQuantity()>0){
                    throw new MyServiceException(String.format("删除失败：物料%s批次%s已经入库了%d件,无法删除",
                            materialBatch.getMaterial().getName(),
                            materialBatch.getBatchNumber(),
                            materialBatch.getStoredQuantity()));
                }
            }
        }
    }

    /**
     * 生成生产批次号
     * 暂时用ID替代
     */
    private String generateBasicBatchNo() {
        return IdUtil.fastSimpleUUID();
    }
}
